//
//  YHComposeToolBar.swift
//  YHWeibo
//
//  Created by yuhao.mac on 16/7/3.
//  Copyright © 2016年 yuhao.mac. All rights reserved.
//

import UIKit

//toolBar的按钮的枚举
enum YHComposeToolBarType: Int{
    //图片
    case Picture = 10
    // @
    case Mention = 20
    // #
    case Trend = 30
    // 表情
    case Emotion = 40
    // +
    case Add = 50
}

class YHComposeToolBar: UIView {
    
    //提取切换键盘的按钮
    var emoticonButton: UIButton?
    
    //告知外界按钮的点击
    var closure:((type:YHComposeToolBarType)->())?
    
    //供外界设置按钮样式
    var isEmotion:Bool = false{
        didSet{
            if isEmotion {
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }else {
                // 显示笑脸
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = RandomColor()
        
        let pictureButton = addChildButtons("compose_toolbar_picture",type: .Picture)
        let mentionButton = addChildButtons("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButtons("compose_trendbutton_background",type: .Trend)
        emoticonButton = addChildButtons("compose_emoticonbutton_background",type: .Emotion)
        let addButton = addChildButtons("compose_add_background",type: .Add)
        
        // 添加约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
    }
    
    //创建按钮的公共方法
    func addChildButtons(imgName: String, type:YHComposeToolBarType) -> UIButton{
        let button = UIButton()
        //枚举值.rawValue可以转成 Int
        button.tag = type.rawValue
        
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }
    
    func buttonClick(button: UIButton){
        closure?(type: YHComposeToolBarType(rawValue: button.tag)!)
    }
}
